function transMat = getTransProb(sampleTotals,snapsPerYear,...
   transInterval,algorithm)

[nRatings1,nRatings2] = size(sampleTotals.totalsMat);
nRatings = nRatings2;
diagInd = logical(eye(nRatings1,nRatings2));

totalsVec = sampleTotals.totalsVec(:);
totalsMat = sampleTotals.totalsMat;

% For duration, 'transMat' temporarily stores the generator matrix
if (strcmp(algorithm,'duration'))
   totalsMat(diagInd) = 0;
   totalsMat(diagInd) = -sum(totalsMat,2);
   transMat = zeros(nRatings,nRatings);
elseif (strcmp(algorithm,'cohort'))
   transMat = eye(nRatings);
end

nonEmpty = find(totalsVec>0);
transMat(nonEmpty,:) = bsxfun(@ldivide,...
   totalsVec(nonEmpty),totalsMat(nonEmpty,:));

% Get the transition matrix with the desired periodicity, in percent
if (strcmp(algorithm,'duration'))
   transMat = expm(transMat*transInterval);
elseif (strcmp(algorithm,'cohort'))
   if (snapsPerYear~=1 || transInterval~=1)
      transMat = transMat^(snapsPerYear*transInterval);
   end
end
transMat = 100*transMat(1:nRatings1,1:nRatings2);